package com.linxuan.reggie.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

/**
 * 发送短信的工具类
 */
@Slf4j
public class SMSUtils {

    /**
     * 发送短信
     *
     * @param signName     签名
     * @param templateCode 模板
     * @param phoneNumbers 手机号
     * @param param        参数
     */
    public static void sendMessage(String signName, String templateCode, String phoneNumbers, String param) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI5tAFoBmA5JatDagLqbdC",
                "RljiMEQyZdrFh6dTfSs0jA9Bu9emCU");
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSysRegionId("cn-hangzhou");
        // 设置接收短线的手机号码
        request.setPhoneNumbers(phoneNumbers);
        // 设置短信的签名名称
        request.setSignName(signName);
        // 设置短信模板CODE
        request.setTemplateCode(templateCode);
        // 设置短信模板变量对应的实际值
        request.setTemplateParam("{\"code\":\"" + param + "\"}");
        try {
            client.getAcsResponse(request);
            log.info("短信发送成功...");
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
